package com.suoluo.zenapp.service.dest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.suoluo.zenapp.common.APPConstants;
import com.suoluo.zenapp.common.CommonResp;
import com.suoluo.zenapp.common.UBB;
import com.suoluo.zenapp.common.file.FileEntry;
import com.suoluo.zenapp.common.file.UploadImageManager;
import com.suoluo.zenapp.dao.dest.DestDAO;
import com.suoluo.zenapp.dao.zentravel.ZenDestDAO;
import com.suoluo.zenapp.pojo.dest.DestExtPOJO;
import com.suoluo.zenapp.pojo.dest.DestImgResourcePOJO;
import com.suoluo.zenapp.pojo.dest.DestResourceExtPOJO;
import com.suoluo.zenapp.pojo.dest.DestResourcePOJO;
import com.suoluo.zenapp.pojo.dest.DestTextResourcePOJO;
import com.suoluo.zenapp.pojo.dest.DestTrafficPOJO;
import com.suoluo.zenapp.pojo.dest.DestVersionPOJO;
import com.suoluo.zenapp.service.ServiceException;
import com.suoluo.zenapp.vo.DestImgNodeVO;
import com.suoluo.zenapp.vo.DestImgResourceVO;
import com.suoluo.zenapp.vo.DestResourceVO;
import com.suoluo.zenapp.vo.DestVO;
import com.suoluo.zenapp.vo.ParameterVO;
import com.suoluo.zenapp.vo.TripDayItemVO;
import com.suoluo.zenapp.vo.TripDayVO;
import com.suoluo.zenapp.vo.TripDestVO;
import com.suoluo.zenapp.vo.TripVO;

public class DestServiceImpl implements DestService {

	@Autowired
	private DestDAO destDAO;
	@Autowired
	private ZenDestDAO zenDestDAO;

	@Override
	public CommonResp saveDest(DestResourceVO dest) {
		// 保存目的地信息
		int destId = destDAO.save(dest);
		// 更新目的地的destId
		destDAO.updateDestDestId(destId);
		//保存目的地扩展信息
		DestExtPOJO destExt = dest.getDestExt();
		destExt.setDestId(destId);
		
		destDAO.saveDestExt(destExt);
		// 保存目的地文本信息
		DestTextResourcePOJO text = new DestTextResourcePOJO();
		text.setResourceDesc(dest.getDestDesc());
		text.setResourceId(destId);
		destDAO.saveDestText(text);
		// 保存图片信息
		DestImgResourcePOJO imgPOJO = new DestImgResourcePOJO();
		imgPOJO.setLeaf(APPConstants.ZERO);

		imgPOJO.setAttachResourceId(destId);

		imgPOJO.setDestId(destId);
		imgPOJO.setImgSummary(dest.getImgSummary());
		imgPOJO.setLayerIndex(APPConstants.ONE);
		FileEntry miniEntry = null;
		try {
			// entry =
			// UploadImageManager.saveDestResourceImg(dest.getOrigImgFile(),
			// destId);
			miniEntry = UploadImageManager.saveDestMiniImg(
					dest.getOrigImgFile(), destId);
			if (miniEntry != null) {
				// 保存原图片
				imgPOJO.setOrigImg(miniEntry.getFilePath());
				// 保存压缩图片
				imgPOJO.setMiniImg(miniEntry.getMiniFilePath());
				imgPOJO.setParentId(APPConstants.ZERO);
				imgPOJO.setResourceType(APPConstants.RESOURCE_DEST);
				destDAO.saveImgResource(imgPOJO);
				return CommonResp.SUCCESS;
			}
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败");
		} catch (Exception e) {
			UploadImageManager.deleteFile(miniEntry);
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败", e);
		}

	}

	@Override
	public void updateDest(DestResourceVO dest) {
		//更新目的地资源信息和文本描述信息
		updateDestResourceAndTextResource(dest);
		//更新目的地扩展信息
		DestExtPOJO destExt = dest.getDestExt();
		int destId = dest.getId();
		destExt.setDestId(destId);
		//判断目的地扩展信息表是否已经保存了目的地信息
		DestExtPOJO extPOJO = destDAO.findDestExtById(destId);
		//未保存，做插入
		if(extPOJO == null)
		{
			destDAO.saveDestExt(destExt);
		}else
		{
			//保存后，做修改
			destDAO.updateDestExt(destExt);
		}
		
		

	}

	/**
	 * 更新目的地资源信息和文本描述信息
	 * @param dest
	 */
	private void updateDestResourceAndTextResource(DestResourceVO dest)
	{
		// 更新资源表基本信息
		destDAO.updateDest(dest);
		// 更新目的地描述信息
		destDAO.updateDestDesc(dest);
		// 更新目的地图片信息
		FileEntry entry = null;
		try {
			DestImgResourcePOJO imgResource = new DestImgResourcePOJO();
			imgResource.setAttachResourceId(dest.getId());
			imgResource.setImgSummary(dest.getImgSummary());
			FileEntry oldEntry = new FileEntry(dest.getOrigImg());
			entry = UploadImageManager.replaceImgAndMiniImg(
					dest.getOrigImgFile(), oldEntry);

			if (entry != null) {
				imgResource.setOrigImg(entry.getFilePath());
				imgResource.setMiniImg(entry.getMiniFilePath());
			} else {
				imgResource.setOrigImg(oldEntry.getFilePath());
				imgResource.setMiniImg(oldEntry.getMiniFilePath());
			}
			destDAO.updateImg(imgResource);
		} catch (Exception e) {
			UploadImageManager.deleteFile(entry);
			throw new ServiceException();
		}
	}
	@Override
	public DestResourceVO findDestById(int destId) {
		return destDAO.findDestById(destId);
	}

	@Override
	public int findDestResourceCount() {

		return destDAO.findDestResourceCount();
	}

	@Override
	public void saveResource(DestResourcePOJO resource) {
		destDAO.save(resource);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findListDest(ParameterVO para) {
		return destDAO.findListDest(para);
	}

	@Override
	public List<DestVersionPOJO> findDestVersion(ParameterVO param) {
		return destDAO.findDestVersion(param);
	}

	@Override
	public DestVersionPOJO findVersionByDestId(int destId) {
		return destDAO.findVersionByDestId(destId);
	}

	@Override
	public void saveVersion(DestVersionPOJO pojo) {
		destDAO.saveVersion(pojo);
	}

	@Override
	public DestVersionPOJO findDestVersionById(int versionId) {
		return destDAO.findVersionById(versionId);
	}

	@Override
	public void updateVersion(DestVersionPOJO version) {
		destDAO.updateVersion(version);
	}

	@Override
	public int findVersionTotal() {
		return destDAO.findVersionTotal();
	}

	@Override
	public int findResourceTypeCount(int destId) {
		return destDAO.findResourceTypeCount(destId);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findListResourceType(int destId) {
		return destDAO.findListResourceType(destId);
	}

	@Override
	public DestResourcePOJO findDestResourceTypeByNameAndDestId(int destId,
			String name) {
		return destDAO.findDestResourceTypeByNameAndDestId(destId, name);
	}

	@Override
	public Integer findMaxOrderByParentId(int destId) {
		return destDAO.findMaxOrderByParentId(destId);
	}

	@Override
	public void updateNodeCount(int destId) {
		destDAO.updateNodeCount(destId);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findListResourceTypeItem(int resourceTypeId) {
		return destDAO.findListResourceTypeItem(resourceTypeId);
	}

	@Override
	public CommonResp saveItem(DestResourceVO destVO) {

		// 保存资源项信息
		int itemId = destDAO.save(destVO);
		// 保存资源项文本信息
		DestTextResourcePOJO text = new DestTextResourcePOJO();
		text.setResourceDesc(destVO.getDestDesc());
		text.setResourceId(itemId);
		destDAO.saveDestText(text);
		// 保存资源项扩展信息
		DestResourceExtPOJO itemPOJO = destVO.getItemExt();
		itemPOJO.setResourceId(itemId);
		destDAO.saveResourceExt(itemPOJO);
		// 保存资源图片
		DestImgResourcePOJO imgPOJO = new DestImgResourcePOJO();
		imgPOJO.setLeaf(APPConstants.ZERO);
		int destId = destVO.getDestId();
		imgPOJO.setDestId(destId);
		imgPOJO.setAttachResourceId(itemId);
		imgPOJO.setImgSummary(destVO.getImgSummary());
		imgPOJO.setLayerIndex(destVO.getLayerIndex());
		FileEntry miniEntry = null;

		try {
			// entry =
			// UploadImageManager.saveDestResourceImg(dest.getOrigImgFile(),
			// destId);
			miniEntry = UploadImageManager.saveDestMiniImg(
					destVO.getOrigImgFile(), destId);
			if (miniEntry != null) {
				// 保存原图片
				imgPOJO.setOrigImg(miniEntry.getFilePath());
				// 保存压缩图片
				imgPOJO.setMiniImg(miniEntry.getMiniFilePath());
				imgPOJO.setParentId(destVO.getParentId());
				imgPOJO.setResourceType(APPConstants.RESOURCE_ITEM);
				destDAO.saveImgResource(imgPOJO);
				// 3.保存成功后更新目的地的子节点数
				updateNodeCount(destId);
				// 4.同步更新父节点的子节点数
				updateNodeCount(destVO.getParentId());
				return CommonResp.SUCCESS;
			}
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败");
		} catch (Exception e) {
			UploadImageManager.deleteFile(miniEntry);
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败", e);
		}

	}

	@Override
	public CommonResp saveDestImgs(DestResourceVO imgVO) {
		// 封装基础数据
		DestImgResourcePOJO imgPOJO = new DestImgResourcePOJO();
		int destId = imgVO.getId();
		imgPOJO.setAttachResourceId(destId);
		imgPOJO.setDestId(destId);
		DestImgResourcePOJO destImgPOJO = destDAO.findImgPOJOByAttachResourceIdAndleaf(destId);
		imgPOJO.setParentId(destImgPOJO.getId());
		imgPOJO.setImgSummary(imgVO.getImgSummary());
		// 查询同一Attach_resource_id下最大的layerIndex
		int maxLayerIndex = destDAO.findMaxLayerIndexId(imgVO.getId());
		int layerIndex = maxLayerIndex + 1;
		imgPOJO.setLayerIndex(layerIndex);
		imgPOJO.setLeaf(APPConstants.ONE);

		imgPOJO.setResourceType(APPConstants.RESOURCE_DEST);
		imgPOJO.setTreeLeafIndex(APPConstants.ZERO);
		imgPOJO.setTreeIndex(APPConstants.ZERO);
		// 保存图片
		FileEntry entry = null;
		try {
			entry = UploadImageManager.saveDestMiniImg(imgVO.getOrigImgFile(),
					destId);
			if (entry != null) {
				imgPOJO.setMiniImg(entry.getMiniFilePath());
				imgPOJO.setOrigImg(entry.getFilePath());
				// 保存到数据库
				destDAO.saveImgResource(imgPOJO);
				// 更新目的地的叶子节点总数
				destDAO.updateLeafCount(destId);
				return CommonResp.SUCCESS;
			}
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败");

		} catch (Exception e) {
			UploadImageManager.deleteFile(entry);
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败", e);
		}

	}

	@Override
	public List<DestImgResourcePOJO> findListImgs(int attachResourceId) {
		return destDAO.findListImgs(attachResourceId);
	}

	@Override
	public CommonResp saveItemImgs(DestResourceVO imgVO) {
		// 封装基础数据
		DestImgResourcePOJO imgPOJO = new DestImgResourcePOJO();
		int destId = imgVO.getDestId();
		int itemId = imgVO.getId();
		int resourceTypeId = imgVO.getParentId();
		imgPOJO.setAttachResourceId(itemId);
		imgPOJO.setDestId(destId);
		DestImgResourcePOJO destImgPOJO = destDAO.findImgPOJOByAttachResourceIdAndleaf(itemId);
		imgPOJO.setParentId(destImgPOJO.getId());
		imgPOJO.setImgSummary(imgVO.getImgSummary());
		// 查询同一Attach_resource_id下最大的layerIndex
		int maxLayerIndex = destDAO.findMaxLayerIndexId(itemId);
		int layerIndex = maxLayerIndex + 1;
		imgPOJO.setLayerIndex(layerIndex);
		imgPOJO.setLeaf(APPConstants.ONE);

		imgPOJO.setResourceType(APPConstants.RESOURCE_ITEM);
		imgPOJO.setTreeLeafIndex(APPConstants.ZERO);
		imgPOJO.setTreeIndex(APPConstants.ZERO);
		// 保存图片
		FileEntry entry = null;
		try {
			entry = UploadImageManager.saveDestMiniImg(imgVO.getOrigImgFile(),
					destId);
			if (entry != null) {
				imgPOJO.setMiniImg(entry.getMiniFilePath());
				imgPOJO.setOrigImg(entry.getFilePath());
				// 保存到数据库
				destDAO.saveImgResource(imgPOJO);
				// 更新目的地的叶子节点总数
				destDAO.updateLeafCount(destId);
				// 更新目的地类别叶子节点总数
				destDAO.updateLeafCount(resourceTypeId);
				// 更新目的地类别项叶子节点总数
				destDAO.updateLeafCount(itemId);
				return CommonResp.SUCCESS;
			}
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败");

		} catch (Exception e) {
			UploadImageManager.deleteFile(entry);
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败", e);
		}

	}

	@Override
	public String findName(int id) {
		return destDAO.findName(id);
	}

	@Override
	public void saveType(DestResourceVO type) {
		// 1.验证是否有重复类别
		int destId = type.getDestId();
		DestResourcePOJO res = this.findDestResourceTypeByNameAndDestId(destId,
				type.getName());
		if (res != null) {
			throw new ServiceException(CommonResp.FAIL_CODE, "类别信息已存在，不可重复添加！");
		}

		// 2.获取该类型所在顺序
		Integer maxOrder = findMaxOrderByParentId(destId);
		if (maxOrder != null) {
			int temp = maxOrder + 1;
			type.setLayerIndex(temp);
		} else {
			maxOrder = APPConstants.ONE;
		}

		type.setLayerIndex(maxOrder);
		type.setNodeCount(APPConstants.ZERO);
		type.setParentId(type.getDestId());
		type.setResourceType(APPConstants.RESOURCE_TYPE);
		type.setTreeIndex(APPConstants.ZERO);
		// 3.保存当前的类别
		int typeId = destDAO.save(type);
		// 4.在目的地下增加了该类别，需要增加该目的地的子节点数
		updateNodeCount(destId);

		// 5.保存该类别的描述信息
		DestTextResourcePOJO text = new DestTextResourcePOJO();
		text.setResourceDesc(type.getDestDesc());
		text.setResourceId(typeId);
		destDAO.saveDestText(text);
		// 6.保存该类别的默认图片信息
		DestImgResourcePOJO imgPOJO = new DestImgResourcePOJO();
		imgPOJO.setLeaf(APPConstants.ZERO);

		imgPOJO.setAttachResourceId(typeId);

		// destId = type.getParentId();
		imgPOJO.setDestId(destId);
		imgPOJO.setImgSummary(type.getImgSummary());

		imgPOJO.setLayerIndex(maxOrder);
		FileEntry miniEntry = null;
		try {
			// entry =
			// UploadImageManager.saveDestResourceImg(dest.getOrigImgFile(),
			// destId);
			miniEntry = UploadImageManager.saveDestMiniImg(
					type.getOrigImgFile(), destId);
			if (miniEntry != null) {
				// 保存原图片
				imgPOJO.setOrigImg(miniEntry.getFilePath());
				// 保存压缩图片
				imgPOJO.setMiniImg(miniEntry.getMiniFilePath());
				imgPOJO.setParentId(destId);
				imgPOJO.setResourceType(APPConstants.RESOURCE_TYPE);
				destDAO.saveImgResource(imgPOJO);
			} else {
				throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败");
			}
		} catch (Exception e) {
			UploadImageManager.deleteFile(miniEntry);
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败", e);
		}
	}

	@Override
	public void updateDestResourcesSort(int destId) {

		// 查询目的地下图片
		List<DestImgResourcePOJO> destImgs = destDAO.findDestAllImages(destId);

		Map<Integer, DestImgNodeVO> destImgNodeIndex = new HashMap<Integer, DestImgNodeVO>();
		List<DestImgNodeVO> leafImgNodeList = new ArrayList<DestImgNodeVO>();

		// 1.收集叶子节点，建立非叶子节点索引
		for (DestImgResourcePOJO img : destImgs) {
			if (img.getLeaf() == 1) {
				leafImgNodeList.add(new DestImgNodeVO(img));
			} else {
				DestImgNodeVO destImgNode = new DestImgNodeVO(img);
				destImgNodeIndex.put(img.getAttachResourceId(), destImgNode);
			}
		}

		// 2.将叶子节点挂载到父节点上
		for (DestImgNodeVO leaf : leafImgNodeList) {
			DestImgNodeVO parent = destImgNodeIndex.get(leaf.getNode()
					.getAttachResourceId());
			parent.addLeaf(leaf);
		}
		// 3.生成目的地树
		for (DestImgNodeVO node : destImgNodeIndex.values()) {
			int parentId = node.getNode().getParentId();
			DestImgNodeVO parent = destImgNodeIndex.get(parentId);
			if (parent != null) {
				parent.addChildNode(node);
			}
		}

		DestImgNodeVO root = destImgNodeIndex.get(destId);
		AtomicInteger treeIndex = new AtomicInteger(0);
		AtomicInteger leafTreeIndex = new AtomicInteger(0);
		AtomicInteger unleafTreeIndex = new AtomicInteger(0);
		// 4.标记顺序
		List<DestResourcePOJO> unleafNodeList = new ArrayList<DestResourcePOJO>();
		markTreeIndex(root, treeIndex, leafTreeIndex,unleafTreeIndex,unleafNodeList);

		// 5.将顺序同步到app_dest_img_resource表
		destDAO.updateResourceImagesIndex(destImgs);
		
		//6.将非叶子节点顺序同步到app_dest_resource
		destDAO.updateResourceTreeIndex(unleafNodeList);
	}

	private void markTreeIndex(DestImgNodeVO treeNode, AtomicInteger treeIndex,
			AtomicInteger leafTreeIndex,AtomicInteger unleafTreeIndex,List<DestResourcePOJO> unleafNodeList) {
		DestImgResourcePOJO node = treeNode.getNode();
		// 标记默认图片的饿顺序
		node.setTreeIndex(treeIndex.incrementAndGet());
		DestResourcePOJO unleafNode = new DestResourcePOJO();
		unleafNode.setId(treeNode.getNode().getAttachResourceId());
		unleafNode.setTreeIndex(unleafTreeIndex.incrementAndGet());
		unleafNodeList.add(unleafNode);
		
		// 标记该节点下的叶子节点的顺序
		Collections.sort(treeNode.getLeafChildren());
		for (DestImgNodeVO leaf : treeNode.getLeafChildren()) {
			DestImgResourcePOJO leafNode = leaf.getNode();
			leafNode.setTreeIndex(treeIndex.incrementAndGet());
			leafNode.setTreeLeafIndex(leafTreeIndex.incrementAndGet());
		}

		// 标记该节点下的子节点的顺序
		Collections.sort(treeNode.getChildren());
		for (DestImgNodeVO childNode : treeNode.getChildren()) {
			markTreeIndex(childNode, treeIndex, leafTreeIndex,unleafTreeIndex,unleafNodeList);
		}
	}

	@Override
	public List<DestResourcePOJO> findListDestName() {

		return destDAO.findListDestName();
	}

	@Override
	public List<DestVO> findDestAllResource(int destId) {
		return destDAO.findDestAllResource(destId);
	}

	@Override
	public List<DestImgResourcePOJO> findDestImg(int destId) {
		return destDAO.findDestImg(destId);
	}

	@Override
	public DestTextResourcePOJO findDestTextResourcePOJOById(int resourceId) {
		return destDAO.findDestTextResourcePOJOById(resourceId);
	}

	@Override
	public DestResourceExtPOJO findDestResourceExtPOJOById(int resourceId) {
		return destDAO.findDestResourceExtPOJOById(resourceId);
	}

	@Override
	public void deleteDestImg(DestImgResourcePOJO img) {
		int imgId = img.getId();
		// 删除数据库记录
		destDAO.deleteImg(imgId);
		// 删除本地图片
		FileEntry entry = new FileEntry(img.getOrigImg());
		UploadImageManager.deleteFile(entry);

		// 更新目的地的叶子节点总数
		int destId = img.getDestId();
		destDAO.updateDestResourceLeafCount(destId);

	}

	@Override
	public void deleteItemImg(DestImgResourcePOJO img) {
		deleteDestImg(img);
		// 更新类别的叶子节点总数
		int typeId = img.getParentId();
		destDAO.updateDestResourceLeafCount(typeId);
		// 更新特色项的叶子节点总数
		int itemId = img.getAttachResourceId();
		destDAO.updateDestResourceLeafCount(itemId);

	}

	@Override
	public void updateItem(DestResourceVO destVO) {
		//更新目的地资源信息和文本描述信息
		updateDestResourceAndTextResource(destVO);
		// 更新资源项扩展信息
		DestResourceExtPOJO extPOJO = destVO.getItemExt();
		extPOJO.setResourceId(destVO.getId());
		destDAO.updateDestResourceExt(extPOJO);

	}
	
//	void updateResource(DestResourcePOJO){
//		
//	}
//	
	@Override
	public DestImgResourcePOJO findDestResourceImgById(int imgId) {
		return destDAO.findDestResourceImg(imgId);
	}

	@Override
	public void updateDestResourceImg(DestImgResourceVO imgVO) {
		FileEntry entry = null;
		try {
			
			MultipartFile tempFile = imgVO.getImgFile();
			if (tempFile.getSize() != 0){
				FileEntry oldEntry = new FileEntry(imgVO.getOrigImg());
				entry = UploadImageManager.replaceImgAndMiniImg(tempFile, oldEntry);
				imgVO.setOrigImg(entry.getFilePath());
				imgVO.setMiniImg(entry.getMiniFilePath());
			}

			destDAO.updateDestResourceImg(imgVO);
		} catch (Exception e) {
			UploadImageManager.deleteFile(entry);
			throw new ServiceException(CommonResp.FAIL_CODE, "图片保存失败", e);
		}
	}

	@Override
	public DestExtPOJO findDestExtById(int destId) {
		
		return destDAO.findDestExtById(destId);
	}

	@Override
	public List<DestTrafficPOJO> findListDestTraffic(int destId) {
		return destDAO.findListDestTraffic(destId);
	}

	@Override
	public void saveDestTraffic(DestTrafficPOJO traffic) {
		// 获取同一目的地下同一交通类别的最大顺序值
		int destId = traffic.getDestId();
		int trafficTypeId = traffic.getTrafficType();
		Integer maxOrder = destDAO.findMaxParagraghOrder(destId,trafficTypeId);
		if(maxOrder == null)
		{
			maxOrder = 1;
		}
		else
		{
			maxOrder = maxOrder + 1;
		}
		traffic.setParagraghOrder(maxOrder);
		destDAO.saveDestTraffic(traffic);
	}

	@Override
	public void updateDestTraffic(DestTrafficPOJO traffic) {
		destDAO.updateDestTraffic(traffic);
		
	}

	@Override
	public void deleteDestTraffic(int trafficId) {
		destDAO.deleteDestTraffic(trafficId);
		
	}

	@Override
	public DestTrafficPOJO findDestTrafficById(int trafficId) {
		return destDAO.findDestTrafficById(trafficId);
	}

	@Override
	public List<DestTrafficPOJO> findListDestTrafficByTypeId(int destId,
			int typeId) {
		return destDAO.findListDestTrafficByTypeId(destId,typeId);
	}

	@Override
	public void updateDestType(DestResourceVO dest) {
		//更新目的地资源信息和文本描述信息
		updateDestResourceAndTextResource(dest);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findZentravelDestByName(String destName) {
		destName = "%"+destName+"%";
		return zenDestDAO.findZentravelDestByName(destName);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findZentravelDestFeature(int destId) {
		return zenDestDAO.findZentravelDestFeature(destId);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findZentravelDestFeatureItem(int featureId,String itemTitle) {
		itemTitle = "%"+itemTitle+"%";
		return zenDestDAO.findZentravelDestFeatureItem(featureId,itemTitle);
	}

	@Override
	public List<TripVO> findListTrips(int destId) {
		return zenDestDAO.findListTrips(destId);
	}


	
	@Override
	public List<TripDestVO> findListTripDest(int tripId) {
		return zenDestDAO.findListTripDest(tripId);
	}
	@Override
	public List<TripDayVO> findListTripDays(int tripId) {
		// 查询行程的每日行程和行程项信息
		return zenDestDAO.findListTripDay(tripId);
		
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<TripDayItemVO> findListTripItem(int tripId) {
		List<TripDayItemVO> listItem = new ArrayList<TripDayItemVO>();
		List<Map> list = zenDestDAO.findListTripDayItem(tripId);
		for (Map map : list) {
			TripDayItemVO tripDaysItemVO = new TripDayItemVO();
			tripDaysItemVO.setTripItemId((Long) map.get("t_trip_item_id"));
			tripDaysItemVO.setCategoryId((Integer) map
					.get("t_feature_category_id"));
			tripDaysItemVO.setCategoryImg((String) map
					.get("t_feature_category_img"));
			tripDaysItemVO.setCategoryName((String) map
					.get("t_feature_category_name"));
			tripDaysItemVO.setTripDayId((Integer)map.get("t_trip_day_id"));
			String preffix = (String) map.get("t_feature_item_img_preffix");
			String imgName = (String) map.get("t_feature_item_img");
			tripDaysItemVO.setItemImg(preffix+imgName);
			tripDaysItemVO.setItemMiniImg(preffix+"mini"+imgName);
			tripDaysItemVO.setPauseTime((Integer) map.get("t_pause_time"));
			tripDaysItemVO.setRelId((Integer) map.get("t_feature_item_id"));
			tripDaysItemVO.setSummary((String) map
					.get("t_feature_item_summary"));
			tripDaysItemVO.setEntitle((String) map.get("t_feature_item_title_en"));
			tripDaysItemVO.setHotelId((Integer) map.get("t_rel_id"));
			tripDaysItemVO.setZhtitle((String) map
					.get("t_feature_item_title_zh"));
			tripDaysItemVO.setTripItemOrder((Integer) map
					.get("t_trip_item_order"));
			tripDaysItemVO.setLat((BigDecimal) map.get("t_lat"));
			tripDaysItemVO.setLng((BigDecimal) map.get("t_lng"));
			String itemDesc = (String) map.get("t_feature_item_desc");
			tripDaysItemVO.setItemDesc(UBB.clear(itemDesc));
			listItem.add(tripDaysItemVO);
		}
		
		return listItem;
	}


}
